﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.SPP.EntidadesNegocio;
using MJM.CBD.Conn;

namespace MJM.SPP.AccesoDatos
{
    public class RestriccionGrupoAD
    {
        public int Insertar(RestriccionGrupoEN obj)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_RESGRU_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmID,prmRestric,prmGrupo;
            int codigo = 0;
            try
            {
                prmID = cmd.CreateParameter();
                prmID.Direction = ParameterDirection.ReturnValue;
                prmID.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(prmID);

                prmRestric = cmd.CreateParameter();
                prmRestric.ParameterName = "@vRESEXT_P_inID";
                prmRestric.SqlDbType = SqlDbType.Int;
                prmRestric.Value = obj.RESEXT_P_inID;
                cmd.Parameters.Add(prmRestric);

                prmGrupo = cmd.CreateParameter();
                prmGrupo.ParameterName = "@vUSUGRU_P_inID";
                prmGrupo.SqlDbType = SqlDbType.Int;
                prmGrupo.Value = obj.USUGRU_P_inID;
                cmd.Parameters.Add(prmGrupo);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                codigo = Convert.ToInt32(prmID.Value);
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                codigo = 0;
                cmd.Dispose();
                conn.Dispose();
            }
            return codigo;
        }

        public int Modificar(RestriccionGrupoEN obj)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_WEB_RESGRU_Modificar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmID, prmRestric, prmGrupo,prmEliminado;            
            int codigo = 1;
            try
            {
                prmID = cmd.CreateParameter();
                prmID.ParameterName = "@vRESGRU_P_inID";
                prmID.SqlDbType = SqlDbType.Int;
                prmID.Value = obj.RESGRU_P_inID;
                cmd.Parameters.Add(prmID);

                prmRestric = cmd.CreateParameter();
                prmRestric.ParameterName = "@vRESEXT_P_inID";
                prmRestric.SqlDbType = SqlDbType.Int;
                prmRestric.Value = obj.RESEXT_P_inID;
                cmd.Parameters.Add(prmRestric);

                prmGrupo = cmd.CreateParameter();
                prmGrupo.ParameterName = "@vUSUGRU_P_inID";
                prmGrupo.SqlDbType = SqlDbType.Int;
                prmGrupo.Value = obj.USUGRU_P_inID;
                cmd.Parameters.Add(prmGrupo);

                prmEliminado = cmd.CreateParameter();
                prmEliminado.ParameterName = "@vRESGRU_inFLAELI";
                prmEliminado.SqlDbType = SqlDbType.Int;
                prmEliminado.Value = obj.RESGRU_inFLAELI;
                cmd.Parameters.Add(prmEliminado);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                codigo = 0;
                cmd.Dispose();
                conn.Dispose();
            }

            return codigo;
        }

        public List<RestriccionGrupoEN> Listar()
        {
            List<RestriccionGrupoEN> lst = new List<RestriccionGrupoEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_RESGRU_listar", conn);
            cmd.CommandType=CommandType.StoredProcedure;
            SqlDataReader dr;

            try
            {
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                RestriccionGrupoEN obj;
                while (dr.Read())
                {
                    obj = new RestriccionGrupoEN();
                    obj.RESGRU_P_inID = dr.GetInt32(0);
                    obj.RESEXT_P_inID= dr.GetInt32(1);
                    obj.USUGRU_P_inID = dr.GetInt32(2);
                    obj.RESGRU_inFLAELI = dr.GetInt32(3);                    
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                return null;
            }
            return lst;
        }
    }
}
